home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / Portable Patmos / usr / include / bitstring.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-08  |  3.3 KB  |  115 lines  |  [TEXT/R*ch]

  1. /*
  2.  * Copyright (c) 1989 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Paul Vixie.
  7.  *
  8.  * Redistribution and use in source and binary forms are permitted
  9.  * provided that the above copyright notice and this paragraph are
  10.  * duplicated in all such forms and that any documentation,
  11.  * advertising materials, and other materials related to such
  12.  * distribution and use acknowledge that the software was developed
  13.  * by the University of California, Berkeley.  The name of the
  14.  * University may not be used to endorse or promote products derived
  15.  * from this software without specific prior written permission.
  16.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  17.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  18.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  19.  *
  20.  *    from: @(#)bitstring.h    5.2 (Berkeley) 4/4/90
  21.  *    $Id: bitstring.h,v 1.3 1993/08/01 18:44:58 mycroft Exp $
  22.  */
  23.  
  24. #ifndef _BITSTRING_H_
  25. #define _BITSTRING_H_
  26.  
  27. /* modified for SV/AT and bitstring bugfix by M.R.Murphy, 11oct91
  28.  * bitstr_size changed gratuitously, but shorter
  29.  * bit_alloc   spelling error fixed
  30.  * the following were efficient, but didn't work, they've been made to
  31.  * work, but are no longer as efficient :-)
  32.  * bit_nclear, bit_nset, bit_ffc, bit_ffs
  33.  */
  34. typedef    unsigned char bitstr_t;
  35.  
  36. /* internal macros */
  37.                 /* byte of the bitstring bit is in */
  38. #define    _bit_byte(bit) \
  39.     ((bit) >> 3)
  40.  
  41.                 /* mask for the bit within its byte */
  42. #define    _bit_mask(bit) \
  43.     (1 << ((bit)&0x7))
  44.  
  45. /* external macros */
  46.                 /* bytes in a bitstring of nbits bits */
  47. #define    bitstr_size(nbits) \
  48.     (((nbits) + 7) >> 3)
  49.  
  50.                 /* allocate a bitstring */
  51. #define    bit_alloc(nbits) \
  52.     (bitstr_t *)calloc((size_t)bitstr_size(nbits), sizeof(bitstr_t))
  53.  
  54.                 /* allocate a bitstring on the stack */
  55. #define    bit_decl(name, nbits) \
  56.     (name)[bitstr_size(nbits)]
  57.  
  58.                 /* is bit N of bitstring name set? */
  59. #define    bit_test(name, bit) \
  60.     ((name)[_bit_byte(bit)] & _bit_mask(bit))
  61.  
  62.                 /* set bit N of bitstring name */
  63. #define    bit_set(name, bit) \
  64.     (name)[_bit_byte(bit)] |= _bit_mask(bit)
  65.  
  66.                 /* clear bit N of bitstring name */
  67. #define    bit_clear(name, bit) \
  68.     (name)[_bit_byte(bit)] &= ~_bit_mask(bit)
  69.  
  70.                 /* clear bits start ... stop in bitstring */
  71. #define    bit_nclear(name, start, stop) { \
  72.     register bitstr_t *_name = name; \
  73.     register int _start = start, _stop = stop; \
  74.     while (_start <= _stop) { \
  75.         bit_clear(_name, _start); \
  76.         _start++; \
  77.         } \
  78. }
  79.  
  80.                 /* set bits start ... stop in bitstring */
  81. #define    bit_nset(name, start, stop) { \
  82.     register bitstr_t *_name = name; \
  83.     register int _start = start, _stop = stop; \
  84.     while (_start <= _stop) { \
  85.         bit_set(_name, _start); \
  86.         _start++; \
  87.         } \
  88. }
  89.  
  90.                 /* find first bit clear in name */
  91. #define    bit_ffc(name, nbits, value) { \
  92.     register bitstr_t *_name = name; \
  93.     register int _bit, _nbits = nbits, _value = -1; \
  94.     for (_bit = 0; _bit < _nbits; ++_bit) \
  95.         if (!bit_test(_name, _bit)) { \
  96.             _value = _bit; \
  97.             break; \
  98.         } \
  99.     *(value) = _value; \
  100. }
  101.  
  102.                 /* find first bit set in name */
  103. #define    bit_ffs(name, nbits, value) { \
  104.     register bitstr_t *_name = name; \
  105.     register int _bit, _nbits = nbits, _value = -1; \
  106.     for (_bit = 0; _bit < _nbits; ++_bit) \
  107.         if (bit_test(_name, _bit)) { \
  108.             _value = _bit; \
  109.             break; \
  110.         } \
  111.     *(value) = _value; \
  112. }
  113.  
  114. #endif /* !_BITSTRING_H_ */
  115.